﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using System.Xml.Linq;
using MDCAdminTool.Controls.Cache_Cluster;

namespace MDCAdminTool.Commands
{
	public class RenameCacheCluster : AsyncCommandBindingBase
	{
		public RenameCacheCluster()
		{
			Command = Application.Current.Resources["RenameCacheCluster"] as ICommand;
		}

		protected override void OnExecuted(object sender, ExecutedRoutedEventArgs e)
		{
			var cluster = e.Parameter as CacheCluster;

			if(cluster == null)
			{
				var @base = e.Parameter as GroupBase;
				if(@base != null)
				{
					cluster = @base.Cluster;
				}
				else
				{
					return;
				}
			}

			RenameWindow renameWindow = null;
			
			Application.Current.Dispatcher.Invoke(
				new Action(
					() =>
						{
							renameWindow = new RenameWindow(cluster.Name);

							renameWindow.ShowDialog();
						}));

			cluster.Name = renameWindow.ClusterName;
            App.Clusters.SaveConfiguration();

		    Application.Current.Dispatcher.Invoke(new Action(() => renameWindow.Close()));

		}
	}
}
